package jfox.platform.aaa.bo;

import java.util.List;
import javax.ejb.EJB;
import javax.ejb.Local;
import javax.ejb.Stateless;

import jfox.platform.aaa.dao.UserDAO;
import jfox.platform.aaa.entity.User;
import jfox.platform.common.Constants;
import jfox.platform.infrastructure.DataAccessObject;
import jfox.platform.infrastructure.EntityNotExistedException;
import jfox.platform.infrastructure.SuperBO;

/**
 * @author <a href="mailto:thaiguo@gmail.com">Taiguo Zhang</a>
 */
@Stateless
@Local
public class UserBOBean extends SuperBO implements UserBO{

    @EJB
    UserDAO userDAO;

    public DataAccessObject getDataAccessObject() {
        return userDAO;
    }

    public User getUserById(long userId) {
        return userDAO.getUserById(userId);
    }

    public User getUserByName(String name) {
        return userDAO.getUserByName(name);
    }

    public User getUserByNameAndPassword(String username, String password) {
        return userDAO.getUserByNameAndPassword(username, password);
    }

    public void newUser(User user) {
        userDAO.insertUser(user);
    }

    public User getAdministrator() {
        return getUserByName(Constants.ADMINISTRATOR_NAME);
    }

    public User getUnkown() {
        return getUserByName(Constants.UNKOWN_NAME);
    }

    public User getGuest() {
        return getUserByName(Constants.GUEST_NAME);
    }

    public List<User> getAllUsers() {
        return userDAO.getAllUsers();
    }

    public int deleteUser(long userId) {
    	
        User user = userDAO.getUserById(userId);        
        if(user == null){
            throw new EntityNotExistedException(User.class, userId);
        }
        // 判断 User 是否被引用
        checkEntityReferenced(user);
        
        return userDAO.deleteUser(userId);
    }

	public int updateUser(User user) {
		return userDAO.updateUser(user);		
	}
}
